import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { ActivatedRoute, Router } from '@angular/router'
import { NzModalService, NzMessageService } from 'ng-zorro-antd'
import { EPaymentBrand } from '@shared/enums/index'
import { PageService } from '@shared/services/page.service'
import { ETabScrollStatus } from '@shared/enums'
import { CommonAcquirerService } from '@shared/services/acquirer.service'
import {
  Acquirer,
  ECChannel,
  ECUPIChannel,
  ECVISChannel,
  ECMCCChannel,
  ECJCBChannel,
  IChannelInfo,
} from '@shared/interfaces/acquirer'
import { DetailTabClass } from '@shared/classes/DetailTabClass'
import {
  getECUPIChannelColumns,
  getECVISChannelColumns,
  getECMCCChannelColumns,
  getECJCBChannelColumns,
  getECKBANKChannelColumns,
} from './helper.service'
import { AcquirerService } from '../../acquirer.service'
import { I18NService } from '@core'
import { ECategoryUrl } from '@shared/interfaces/trade'
import { ecChannelSorter } from 'app/utils/helpers'

@Component({
  selector: 'app-acquirer-detail-tab-channel-ec',
  templateUrl: './tab-channel-ec.component.html',
})
export class AcquirerDetailTabChannelEcComponent extends DetailTabClass<Acquirer> implements OnInit {
  acquirer: Acquirer

  channelInfos: IChannelInfo[] = []

  fields = {
    ChannelEC: '1',
  }

  constructor(
    public modelService: NzModalService,
    public msg: NzMessageService,
    public location: Location,
    public pageService: PageService,
    public router: Router,
    public route: ActivatedRoute,
    public acquirerService: AcquirerService,
    private commonAcquirerService: CommonAcquirerService,
    private i18n: I18NService,
  ) {
    super(pageService, route)
  }

  ngOnInit() {
    this.initialData()
  }

  initialData() {
    this.commonAcquirerService.viewAcquirer$.value$.subscribe(acquirer => {
      const { ecPayChannels } = acquirer
      this.acquirer = acquirer
      this.initColumns(ecPayChannels)
    })
    this.pageService.setTabScrollStatus(ETabScrollStatus.Scroll)
  }

  initColumns(ECChannels: ECChannel[]): void {
    // 每次都清空
    this.channelInfos = []
    ECChannels.sort((a: ECChannel, b: ECChannel) => ecChannelSorter(a, b)).forEach(channel => {
      switch (channel.chanCode) {
        case EPaymentBrand.UPI:
          this.initEcUPIColumns(channel as ECUPIChannel)
          break
        case EPaymentBrand.Visa:
          this.initEcVISColumns(channel as ECVISChannel)
          break
        case EPaymentBrand.MasterCard:
          this.initEcMCCColumns(channel as ECMCCChannel)
          break
        case EPaymentBrand.JCB:
          this.initEcJCBColumns(channel as ECJCBChannel)
          break
        case EPaymentBrand.KBank:
          this.initEcKBANKColumns(channel as ECVISChannel)
          break
      }
    })
  }

  initEcUPIColumns(channel: ECUPIChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/upi-logo.png',
      channelName: this.trans('PAYMENT_BRAND.UPI'),
      channelColumns: getECUPIChannelColumns(channel),
    })
  }

  initEcKBANKColumns(channel: ECVISChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/kbank.jpg',
      channelName: 'KBank',
      channelColumns: getECKBANKChannelColumns(channel),
    })
  }

  initEcVISColumns(channel: ECVISChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/Visa-logo.svg',
      channelName: this.trans('PAYMENT_BRAND.VIS'),
      channelColumns: getECVISChannelColumns(channel),
    })
  }

  initEcMCCColumns(channel: ECMCCChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/Mcc-logo.svg',
      channelName: this.trans('PAYMENT_BRAND.MCC'),
      channelColumns: getECMCCChannelColumns(channel),
    })
  }

  initEcJCBColumns(channel: ECJCBChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/jcb-logo.png',
      channelName: this.trans('PAYMENT_BRAND.JCB'),
      channelColumns: getECJCBChannelColumns(channel),
    })
  }

  goDelete(index: number): void {
    this.modelService.confirm({
      nzTitle: this.trans('GENERAL.NOTIFICATION.WARNING'),
      nzContent: this.trans('BIZ.ACQUIRER.OTHER.QR_CHANNEL.DELETE_CHANNEL_CONFIRM'),
      nzOnOk: () => this.deleteChannel(index),
    })
  }

  deleteChannel(index: number): void {
    const insCode = this.acquirer.institution.common.insCode
    const channelId = this.acquirer.ecPayChannels[index].id
    this.acquirerService.deleteEcChannelForAcquirer(ECategoryUrl.ec, insCode, channelId).subscribe(isSuccess => {
      if (isSuccess) {
        this.msg.info(this.trans('GENERAL.NOTIFICATION.DELETE_SUCCESS'))
        this.acquirer.ecPayChannels = this.acquirer.ecPayChannels.filter(c => c.id !== channelId)
        this.commonAcquirerService.setViewingAcquirer(this.acquirer)
      }
    })
  }

  goAddChannel(): void {
    this.router.navigate(['../../ec/add'], { relativeTo: this.route })
  }

  // abstract methods
  onCancel() {}

  onSubmit() {
    const columns = this.columns[this.field][this.rowIndex]
  }

  initialColumns() {}

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
